package gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.*;

import api.storage.DataStorageCSV;

/**
 * @author Ann Raboschuk
 */
public class MVC
{
   private static final int  DEFAULT_WIDTH    = 700;
   private static final int  DEFAULT_HEIGHT   = 250;
   private JFrame frame;
   private Controller controller;
   private View view;
   private JFileChooser chooser;
   
   public MVC()
   {
      frame = new JFrame("Persons");
      frame.setResizable(false);
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      frame.setBounds((screenSize.width - DEFAULT_WIDTH) / 2,
            (screenSize.height - DEFAULT_HEIGHT) / 2, DEFAULT_WIDTH, DEFAULT_HEIGHT);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      controller = new Controller();
      view = new View(controller);
      frame.setJMenuBar(createMenu());
      frame.setContentPane(view);
      
      chooser = new JFileChooser();
      chooser.setCurrentDirectory(new File("."));
   }
   
   public JMenuBar createMenu()
   {
      JMenuBar menuBar = new JMenuBar();
      JMenu loadMenu = new JMenu("Load");
      loadMenu.setMnemonic('L');
      
      JMenuItem menuItem = new JMenuItem("CSV...");
      menuItem.addActionListener(new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent e)
         {
            if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION)
            {
               try
               {
                  DataStorageCSV ds = new DataStorageCSV(chooser
                        .getSelectedFile().getAbsolutePath());
                  controller.setPeople(ds.load());
                  view.redraw();
               }
               catch (Exception ex)
               {
                  ex.printStackTrace();
               }
            }
         }
      });
      loadMenu.add(menuItem);
      menuBar.add(loadMenu);
      
      JMenu saveMenu = new JMenu("Save");
      saveMenu.setMnemonic('S');
      menuItem = new JMenuItem("CSV...");
      menuItem.addActionListener(new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent e)
         {
            if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION)
            {
               try
               {
                  DataStorageCSV ds = new DataStorageCSV(chooser
                        .getSelectedFile().getAbsolutePath());
                  controller.setPeople(ds.load());
                  view.redraw();
               }
               catch (Exception ex)
               {
                  ex.printStackTrace();
               }
            }
         }
      });
      menuBar.add(saveMenu);
      
      JMenu menu = new JMenu("About");
      menu.addMouseListener(new MouseAdapter()
      {
         @Override
         public void mousePressed(MouseEvent e)
         {
            JOptionPane.showMessageDialog(frame,
                  "This programm is the product of Raboshuk&Uryadkina work", "About",
                  JOptionPane.INFORMATION_MESSAGE);
         }
      });
      menuBar.add(menu);
      return menuBar;
   }
   
   public static int getDefaultWidth()
   {
      return DEFAULT_WIDTH;
   }
   
   public void show()
   {
      frame.setVisible(true);
   }
   
   public static void main(String[] args)
   {
      new MVC().show();
   }
}
